package com.ng.framework.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Pablo Abbate
 * @module Arquitectura
 * 
 */
public class NGSearchCriteria implements Serializable {

	public static final String SORT_ORDER_ASC = "asc";

	public static final String SORT_ORDER_DESC = "desc";

	private static final long serialVersionUID = 1194790644910309299L;

	private final List<NGSearchParameter> bpSearchParameters = new ArrayList<NGSearchParameter>();

	private Integer limit;

	private Integer offset;

	private String sortField;

	private String sortOrder;

	public NGSearchCriteria(Integer limit, Integer offset, String sortField,
			String sortOrder) {
		super();
		this.limit = limit;
		this.offset = offset;
		this.sortField = sortField;
		this.sortOrder = sortOrder;
	}

	public NGSearchCriteria() {
		super();
	}

	public String getFrom() {
		if (this.bpSearchParameters.size() == 0) {
			return "";
		}
		final StringBuilder query = new StringBuilder();
		for (final Iterator<NGSearchParameter> iterator = this.bpSearchParameters
				.iterator(); iterator.hasNext();) {
			final NGSearchParameter bpSearchParameter = iterator.next();
			String customFromHQL = bpSearchParameter.getCustomFromHQL();
			if (!customFromHQL.isEmpty()) {
				query.append(" , ");
				query.append(bpSearchParameter.getCustomFromHQL());
			}

		}

		return query.toString();

	}

	public String getWhere() {
		if (this.bpSearchParameters.size() == 0) {
			return "";
		}
		final StringBuilder query = new StringBuilder();
		for (final Iterator<NGSearchParameter> iterator = this.bpSearchParameters
				.iterator(); iterator.hasNext();) {
			final NGSearchParameter bpSearchParameter = iterator.next();
			query.append(bpSearchParameter.getCustomWhereHQL());
			query.append(" AND ");
		}

		return query.substring(0, query.length() - 5).toString();

	}
	public List<NGSearchParameter> addNGSearchParameter(
			final NGSearchParameter bpSearchParameter) {
		this.bpSearchParameters.add(bpSearchParameter);
		return this.bpSearchParameters;
	}

	public int getBPSearchParametersSize() {
		return this.bpSearchParameters.size();
	}

	public Integer getLimit() {
		return limit;
	}

	public Integer getOffset() {
		return offset;
	}

	public String getSortField() {
		return sortField;
	}

	public String getSortOrder() {
		return sortOrder;
	}

}
